package com.ums.providerboot.service;

import org.apache.log4j.Logger;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.SessionScope;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
public class OrderService {

    private final Logger logger = Logger.getLogger(getClass());

    @Resource
    private DiscoveryClient client;

    @GetMapping(value = "/hello")
    public String hello(HttpServletRequest request) {
        logger.info(String.format("session id: %s", request.getSession().getId()));
        List<ServiceInstance> instances = client.getInstances("hello-service");
        for (int i = 0; i < instances.size(); i++) {
            logger.info("/hello,host:" + instances.get(i).getHost() + ",service_id:" + instances.get(i).getServiceId());
        }
        return "Hello World";
    }

    @PostMapping(value = "/placeOrder")
    public Map placeOrder(@RequestBody String body, HttpServletRequest request){
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = headerNames.nextElement();
            String value = request.getHeader(key);
            System.out.println(String.format("key:%s, value:%s", key, value));
        }
        logger.info("session id: "+ request.getSession().getId());
        logger.info("body:"+body);
        List<ServiceInstance> instances = client.getInstances("hello-service");
        for (int i = 0; i < instances.size(); i++) {
            logger.info("/placeOrder, host:" + instances.get(i).getHost() + ",service_id:" + instances.get(i).getServiceId());
        }
        Map map = new HashMap();
        map.put("errCode","0000");
        map.put("errInfo","下单成功,"+new Date());
        map.put("body",body);
        return map;
    }

    @GetMapping(value = "/get_session")
    public Map get_session(@RequestHeader HttpHeaders httpHeaders, HttpServletRequest request){
        if(request.getSession() != null){
            Object user_name = request.getSession().getAttribute("user_name");
            if(user_name!=null){
                System.out.println("user_name = " + user_name+", session id:"+ request.getSession().getId());
            }else{
                System.out.println("can not get");
            }
        }

        System.out.println(httpHeaders);
        //System.out.println("request.getSession().getId() = " + request.getSession().getId());
        Map map = new HashMap();
        map.put("timestamp", new Date());
        //map.put("session id", request.getSession().getId());
        return map;
    }
}
